 /**
 * Description: 用户角色业务实现类
 * Copyright:Copyright 2018 nfky.com. All rights reserved
 * @author:xk
 * @since:0.0.1
 * Create at:2018-01-10 下午 19:40:19
 *  
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2018-01-10   xk       0.0.1     Initial
 */
package com.metro.activation.sys.service.impl;

import com.metro.core.dao.IMapper;
import com.metro.core.service.BaseServiceSupport;
import com.metro.core.service.ServiceException;
import com.metro.core.vo.ResultCode;
import com.metro.core.vo.inf.IResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.metro.activation.sys.mapper.ISysUserRoleMapper;
import com.metro.activation.sys.service.inf.ISysUserRoleService;
import com.metro.activation.sys.beans.vo.SysUserRole;

import java.util.List;

 /**
 * 用户角色业务实现类<br>
 *
 * @author xk
 * @since [0.0.1]
 * @version [0.0.1, 2018-01-10]
 */
@Service("sysUserRoleService")
public class SysUserRoleService extends BaseServiceSupport<SysUserRole> implements ISysUserRoleService{
    //用户角色Mapper
    @Autowired
    private ISysUserRoleMapper sysUserRoleMapper;

    @Override
    public IMapper getMapper() {
        return sysUserRoleMapper;
    }

    @Override
    public IResult save(SysUserRole entity) {
        try {
            sysUserRoleMapper.save(entity);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("新增用户角色失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult update(SysUserRole entity) {
        try {
            sysUserRoleMapper.update(entity);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("修改用户角色失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult delete(Long id) {
        try {
            sysUserRoleMapper.delete(id);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult deleteByIds(String ids) {
        try {
            sysUserRoleMapper.deleteByIds(ids);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

     @Override
     public IResult createUserRole(SysUserRole entity) throws ServiceException {
         try {
             //删除用户现有的角色
             sysUserRoleMapper.deleteByUserIds(entity);
             List<SysUserRole> userRoles = entity.buildUserRoles();
             //批量保存用户角色信息
             sysUserRoleMapper.batchSaveUserRole(userRoles);
             return ResultCode.SUCCESS;
         } catch (Exception e) {
             log.error("新增角色对应关系失败", e);
             throw new ServiceException(ResultCode.ERROR);
         }
     }
 }